package com.vvlive.caibao.smartcity.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.app.SmartCityApp;
import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.base.StringHttpCallBack;
import com.vvlive.caibao.smartcity.bean.MeetingByMonth;
import com.vvlive.caibao.smartcity.utils.DialogUtil;
import com.vvlive.caibao.smartcity.utils.MeetingsController;
import com.vvlive.caibao.smartcity.view.CommonActionBar;

import org.kymjs.kjframe.ui.BindView;

import java.util.HashMap;
import java.util.Map;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/9/7.
 * 会议详情
 */
public class MeetingDetailsActivity extends BaseActivity {

    public static final String DETAILS_KEY = "meeting";

    public static final int EDIT_REQUEST_CODE = 200;
    public static final int EDIT_RESULT_CODE = 201;

    /**
     * 删除会议的返回状态
     */
    private static HashMap<String, String> states = new HashMap<>();

    // 进行删除会议返回状态的初始化
    static {
        states.put("OK", "删除会议成功");
        states.put("DEL_ERROR", "删除会议失败");
        states.put("LOGIN_ERROR", "用户账号或密码错误");
    }

    /**
     * 用于接收要默认显示的会议的信息
     */
    private MeetingByMonth mBean;

    @BindView(id = R.id.details_topic)
    private TextView mTopic;
    @BindView(id = R.id.details_start_time)
    private TextView mStartTime;
    @BindView(id = R.id.details_end_time)
    private TextView mEndTime;
    @BindView(id = R.id.details_address)
    private TextView mAddress;
    @BindView(id = R.id.details_scroll_text)
    private TextView mScrollText;
    @BindView(id = R.id.details_more)
    private TextView mMore;

    @BindView(id = R.id.details_edit, click = true)
    private Button mEdit;
    @BindView(id = R.id.details_del, click = true)
    private Button mDelete;

    @BindView(id = R.id.details_ctrl)
    private LinearLayout mDetailsCtrl;

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_meeting_details;
    }

    @Override
    public void initWidget() {
        CommonActionBar actionBar = getCommonActionBar();
        actionBar.setMoreVisible(false);

        mBean = (MeetingByMonth) getIntent().getSerializableExtra(DETAILS_KEY);

        Log.i("EditMeetingActivity", "mBean = " + mBean);

        // 设置显示会议主题
        String topic = Init.getTopic(mBean.getTopic());
        mTopic.setText(topic);

        // 设置显示开始和结束时间
        String time = mBean.getTime();
        String startTime = Init.getStartTime(time);
        String endTime = Init.getEndTime(time);
        mStartTime.setText(startTime);
        mEndTime.setText(endTime);

        // 设置显示会议室
        String room = Init.getRoom(mBean.getRoom());
        mAddress.setText(room);

        // 设置显示滚动文字
        String scrollText = Init.getScrollText(mBean.getScroolltext());
        mScrollText.setText(scrollText);

        String username = mBean.getUsername();
        if (username != null) {
            if (!mBean.getUsername().equals(SmartCityApp.currentUser.getObjectId())) {
                mDetailsCtrl.setVisibility(View.INVISIBLE);
            }
        }

    }

    /**
     * 初始化显示信息
     */
    final static class Init {

        /**
         * 获取会议主题
         *
         * @param topic
         * @return
         */
        public static String getTopic(String topic) {
            if (null == topic) {
                return "未知会议主题";
            }
            return getTargetString(topic, "：");
        }

        /**
         * 获取会议的开始时间
         *
         * @param time
         * @return
         */
        public static String getStartTime(String time) {
            if (null == time) {
                return "未知开始时间";
            }
            return getTime(time, 0);
        }

        /**
         * 获取会议的结束时间
         *
         * @param time
         * @return
         */
        public static String getEndTime(String time) {
            if (null == time) {
                return "未知结束时间";
            }
            return getTime(time, 1);
        }

        /**
         * 获取会议的开始或者结束时间
         *
         * @param time
         * @param part
         * @return
         */
        private static String getTime(String time, int part) {
            if (time.indexOf("_") > -1) {
                if (part == 0) {
                    return MeetingsController.getStartDateTime(time);
                }
                if (part == 1) {
                    return MeetingsController.getEndDateTime(time);
                }
            }
            return getTargetString(time, "：").split("~")[part];
        }

        /**
         * 获取会议室
         *
         * @param room
         * @return
         */
        public static String getRoom(String room) {
            if (null == room) {
                return "未知会议室";
            }
            return getTargetString(room, "：");
        }

        /**
         * 获取滚动文字
         *
         * @param scrollText
         * @return
         */
        public static String getScrollText(String scrollText) {
            if (null == scrollText) {
                return "未知滚动文字";
            }
            return getTargetString(scrollText, "：");
        }

        /**
         * 获取目标文本
         *
         * @param base
         * @param syb
         * @return
         */
        private static String getTargetString(String base, String syb) {
            int index = base.indexOf(syb);
            if (index > -1) {
                base = base.substring(index + 1);
            }
            return base;
        }
    }

    private MeetingsController mController = new MeetingsController();
    private ProgressDialog mProgress;

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        int id = v.getId();
        switch (id) {
            case R.id.details_del:
                mProgress = DialogUtil.showProgressDialog(this, "", "正在删除");
                mController.deleteMeeting(mBean.getMeetingid(), mCallBack);
                break;
            case R.id.details_edit:
                Intent intent = new Intent(this, EditMeetingActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("obj", mBean);
                intent.putExtras(bundle);
                startActivityForResult(intent, EDIT_REQUEST_CODE);
                break;
        }
    }

    private StringHttpCallBack mCallBack = new StringHttpCallBack() {

        @Override
        public void onSuccess(String data) {
            if (!states.isEmpty()) {
                for (Map.Entry<String, String> entry : states.entrySet()) {
                    if (entry.getKey().equals(data)) {
                        toast(entry.getValue());
                        break;
                    }
                }
            }
            if (mProgress != null) {
                mProgress.dismiss();
            }
        }

        @Override
        public void onError(int errNo, String str) {
            toast("删除失败，请检查网络连接");
            if (mProgress != null) {
                mProgress.dismiss();
            }
        }

    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == EDIT_REQUEST_CODE) {
            if (resultCode == EDIT_RESULT_CODE) {
                log("EDIT_RESULT", "编辑完成，返回会议详情页面");
                setResult(1000);
                finish();
            }
        }
    }
}
